package com.ruoyi.pingan.fymodule.goodsinfo.goodscategory.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.pingan.fymodule.goodsinfo.goods.api.SxoGoodsCategoryApi;
import com.ruoyi.pingan.fymodule.goodsinfo.goods.vo.SxoGoodsCategoryApiVo;
import com.ruoyi.pingan.fymodule.goodsinfo.goods.vo.SxoGoodsCategoryJoinApi;
import com.ruoyi.pingan.fymodule.goodsinfo.goodscategory.entity.SxoGoodsCategory;
import com.ruoyi.pingan.fymodule.goodsinfo.goodscategory.service.SxoGoodsCategoryService;
import com.ruoyi.pingan.fymodule.goodsinfo.goodscategoryjoin.entity.SxoGoodsCategoryJoin;
import com.ruoyi.pingan.fymodule.goodsinfo.goodscategoryjoin.service.SxoGoodsCategoryJoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SxoGoodsCategoryProvider implements SxoGoodsCategoryApi {
    @Autowired
    private SxoGoodsCategoryService sxoGoodsCategoryService;
    @Autowired
    private SxoGoodsCategoryJoinService sxoGoodsCategoryJoinService;

    @Override
    public SxoGoodsCategoryApiVo findGoodsCategory(String id) {
        SxoGoodsCategory sysUser = sxoGoodsCategoryService.getById(id);
        if (ObjectUtil.isNotEmpty(sysUser)) {
            SxoGoodsCategoryApiVo vo = new SxoGoodsCategoryApiVo();
            BeanUtil.copyProperties(sysUser, vo);
            return vo;
        }
        return null;
    }

    @Override
    public List<SxoGoodsCategoryApiVo> listGoodsCategory(List<String> id) {
        List<SxoGoodsCategory> list = sxoGoodsCategoryService.listByIds(id);
        if (list.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        return list.stream().map(sysUser -> {
            SxoGoodsCategoryApiVo vo = new SxoGoodsCategoryApiVo();
            BeanUtil.copyProperties(sysUser, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SxoGoodsCategoryApiVo> listTopGoodsCategory(List<String> ids) {

        List<SxoGoodsCategoryApiVo> childList = new ArrayList<>();
        //List<String> ids = joinList.stream().map(SxoGoodsCategoryJoin::getCategoryId).toList();
        List<SxoGoodsCategory> child = sxoGoodsCategoryService.listByIds(ids);
        for (SxoGoodsCategory vo : child) {
            if (vo.getPid().equals("0")) {
                SxoGoodsCategoryApiVo apiVo = new SxoGoodsCategoryApiVo();
                BeanUtil.copyProperties(vo, apiVo);
                childList.add(apiVo);
            } else {
                long count = childList.stream().filter(o -> o.getId().equals(vo.getPid())).count();
                if (count == 0) {
                    SxoGoodsCategory parent = sxoGoodsCategoryService.getById(vo.getPid());
                    if (parent != null) {
                        SxoGoodsCategoryApiVo parentVo = new SxoGoodsCategoryApiVo();
                        BeanUtil.copyProperties(parent, parentVo);
                        childList.add(parentVo);
                    }
                }
            }
        }

        return childList;
    }

    @Override
    public List<SxoGoodsCategoryJoinApi> listGoodsCategoryJoin(List<String> id) {
        LambdaQueryWrapper<SxoGoodsCategoryJoin> joinWrapper = new LambdaQueryWrapper<>();
        joinWrapper.in(SxoGoodsCategoryJoin::getGoodsId, id);
        List<SxoGoodsCategoryJoin> joinList = sxoGoodsCategoryJoinService.list(joinWrapper);
        if (joinList.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        return joinList.stream().map(join -> {
            SxoGoodsCategoryJoinApi apiVo = new SxoGoodsCategoryJoinApi();
            BeanUtil.copyProperties(join, apiVo);
            return apiVo;
        }).collect(Collectors.toList());
    }

    @Override
    public SxoGoodsCategoryJoinApi findGoodsCategoryJoin(String id) {
        LambdaQueryWrapper<SxoGoodsCategoryJoin> joinWrapper = new LambdaQueryWrapper<>();
        joinWrapper.eq(SxoGoodsCategoryJoin::getGoodsId, id);
        SxoGoodsCategoryJoin join = sxoGoodsCategoryJoinService.getOne(joinWrapper);
        SxoGoodsCategoryJoinApi apiVo = new SxoGoodsCategoryJoinApi();
        BeanUtil.copyProperties(join, apiVo);
        return apiVo;

    }
}
